﻿using IdentityModel;
using IdentityServer4;
using IdentityServer4.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Identity.API.IdentityConfig
{
    public class Config
    {
        public static IEnumerable<IdentityResource> GetIdentityResources =>
            new List<IdentityResource>
            {
                new IdentityResources.OpenId(),
                new IdentityResources.Profile()
            };

        public static IEnumerable<ApiScope> GetApiScopes =>
            new List<ApiScope>
            {
                new ApiScope("basket","购物车服务"),
                new ApiScope("market", "商品服务"),
                new ApiScope("orders", "订单服务"),
                new ApiScope("payments", "支付服务"),
                new ApiScope("mvc", "MVC网站")
            };

        public static IEnumerable<ApiResource> GetApiResources =>
            new List<ApiResource>
            {
                new ApiResource("basket","购物车服务")
                {
                    Scopes = {"basket"}
                },
                new ApiResource("market","商品服务")
                {
                    Scopes = {"market"}
                },
                new ApiResource("orders", "订单服务")
                {
                    Scopes = {"orders"}
                },
                new ApiResource("payments", "支付服务")
                {
                    Scopes = {"payments"}
                }
            };

        public static IEnumerable<Client> GetClients =>
            new List<Client>
            {
                new Client
                {
                    ClientId = "postman client",
                    ClientName = "PostMan Client 连接测试用",

                    AllowedGrantTypes = GrantTypes.ClientCredentials,
                    ClientSecrets = { new Secret("postman secret".Sha256()) },
                    AllowedScopes = { "basket" }
                },
                new Client
                {
                    ClientId = "mvc",
                    ClientName = "MVC Client",
                    AllowedGrantTypes = GrantTypes.Hybrid,
                    ClientSecrets = new List<Secret>
                    {
                        new Secret("49C1A7E1-0C79-4A89-A3D6-A37998FB86B0".Sha256())
                    },
                    ClientUri = "http://host.docker.internal:8000/",
                    AllowAccessTokensViaBrowser = false,
                    RequirePkce = false,
                    RequireConsent = false,
                    AllowOfflineAccess = true,
                    AlwaysIncludeUserClaimsInIdToken = true,
                    AccessTokenLifetime = 120,
                    IdentityTokenLifetime = 120,
                    
                    RedirectUris = new List<string>
                    {
                        "http://host.docker.internal:5000/signin-oidc"
                    },
                    PostLogoutRedirectUris = new List<string>
                    {
                        "http://host.docker.internal:5000/signout-callback-oidc"
                    },
                    AllowedScopes = new List<string>
                    {
                        IdentityServerConstants.StandardScopes.OpenId,
                        IdentityServerConstants.StandardScopes.Profile,
                        IdentityServerConstants.StandardScopes.OfflineAccess,
                        "basket",
                        "market",
                        "orders",
                        "payments",
                        "mvc"
                    }
                }
            };
    }
}
